﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Events;
using Hockey.Shared;
using IO = System.IO;
using System.Reflection;

namespace Hockey.UILayer.Forms
{
    /// <summary>
    /// Interaction logic for HelpForm.xaml
    /// </summary>
    public partial class HelpForm : Window
    {
        private IEventAggregator _events;
        private const string documentPath = "AppData/HockeyHelp.rtf";

        /// <summary>
        /// Constructor
        /// </summary>
        public HelpForm(IEventAggregator events)
        {
            InitializeComponent();

            if (events == null)
                throw new ArgumentNullException("Events");
            _events = events;

            _events.GetEvent<CloseForm>().Subscribe((obj) =>
                {
                    if (obj == this.DataContext)
                    {
                        Close();
                    }
                }, true);

            LoadDocument();
        }

        /// <summary>
        /// Loads the help document
        /// </summary>
        private void LoadDocument()
        {
            try
            {
                // create path
                string appPath = IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                string docPath = IO.Path.Combine(appPath, documentPath);

                // open the file
                using (IO.FileStream stream = new IO.FileStream(docPath, IO.FileMode.Open, IO.FileAccess.Read))
                {
                    // create document
                    FlowDocument doc = new FlowDocument();
                    TextRange range = new TextRange(doc.ContentStart, doc.ContentEnd);

                    // load document
                    range.Load(stream, DataFormats.Rtf);

                    // close file
                    stream.Close();

                    uxRtf.Document = doc;
                }
            }
            catch
            {
            }
        }

    }
}
